package com.spring.sunflower.util;

import android.content.Context;
import android.media.MediaPlayer;

import com.spring.sunflower.App;
import com.spring.sunflower.R;

public class CallRingtoneUtils {
    private static CallRingtoneUtils callRingtoneUtils;

    private MediaPlayer mediaPlayer;
    private static Context mContext;

    public static CallRingtoneUtils getInstance(Context context) {
        mContext = context;
        if (callRingtoneUtils == null)
            synchronized (CallRingtoneUtils.class) {
                if (callRingtoneUtils == null) {
                    callRingtoneUtils = new CallRingtoneUtils();
                }
            }
        return callRingtoneUtils;
    }

    public void playCallRingtone() {
        reset();
        mediaPlayer = MediaPlayer.create(mContext, R.raw.call);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
    }

    public void playHangUp() {
        reset();
        mediaPlayer = MediaPlayer.create(mContext, R.raw.hangup);
        mediaPlayer.setLooping(false);
        mediaPlayer.start();
    }

    public void playHangUp(MediaPlayer.OnCompletionListener paramOnCompletionListener) {
        reset();
        mediaPlayer = MediaPlayer.create(mContext, R.raw.hangup);
        mediaPlayer.setLooping(false);
        mediaPlayer.start();
        mediaPlayer.setOnCompletionListener(paramOnCompletionListener);
    }

    public void playMessageNotification() {
        reset();
        mediaPlayer = MediaPlayer.create(mContext, R.raw.message_notification);
        mediaPlayer.setLooping(false);
        mediaPlayer.start();
    }

    public void playAccept() {
        reset();
        mediaPlayer = MediaPlayer.create(mContext, R.raw.receive);
        mediaPlayer.setLooping(false);
        this.mediaPlayer.start();
    }

    public void releaseMediaPlayer() {
        stop();
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
        if (mContext != null) {
            mContext = null;
        }
    }

    public void reset() {
        if (mediaPlayer != null)
            mediaPlayer.reset();
    }

    public void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying())
            this.mediaPlayer.stop();
    }
}